package com.snail.readyvr.util;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.UUID;

/**
 * Created by wangyunpeng on 2016/3/10.
 */
public class AppUtils {

    /**
     * 根据pacagename判断应用有无安装
     *
     * @param context
     * @param uri
     * @return
     */
    public static boolean isAppInstalled(Context context, String uri) {
        boolean installed = false;
        if (context == null || StringUtils.isNull(uri))
            return installed;
        PackageManager pm = context.getPackageManager();

        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            installed = true;
        } catch (PackageManager.NameNotFoundException e) {
            installed = false;
        }
        return installed;
    }

    /**
     * 安装APP
     *
     * @param context
     * @param fileName
     */
    public static void installApp(Context context, String fileName) {
        if (context == null || StringUtils.isNull(fileName))
            return;
        File file = new File(fileName);
        if (!file.exists()) {
            ToastUtils.show(context, "文件已被删除，删除任务后，重新下载！", Toast.LENGTH_SHORT);
            return;
        }
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(fileName)),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

    /**
     * 打开另外的app
     *
     * @param context
     * @param packageName
     */
    public static void openOtherApp(Context context, String packageName) {
        if (context == null || StringUtils.isNull(packageName))
            return;
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(packageName);
        if (intent != null)
            context.startActivity(intent);

    }

    /**
     * 获得设备号
     *
     * @param context
     * @return
     */
    public static String getIMEI(Context context) {

        try {
            TelephonyManager telephonymanager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            return telephonymanager.getDeviceId();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获得手机号码
     *
     * @param context
     * @return
     */

    public static String getTEL(Context context) {
        TelephonyManager telephonymanager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return telephonymanager.getLine1Number();
    }

    /**
     * 获得设备型号
     *
     * @return
     */

    @SuppressWarnings("static-access")
    public static String getInfo() {
        Build bd = new Build();
        return bd.MANUFACTURER + " " + bd.MODEL;
    }

    /**
     * 获得操作系统版本
     *
     * @return
     */

    public static String getVersionRelease() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 获得App版本号
     *
     * @param context
     * @return
     */

    public static int getVersionCode(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            return info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 获得App版本号
     *
     * @param context
     * @return
     */

    public static String getVersion(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            return info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "获得版本错误";
        }
    }

    /**
     * 获得设备类型
     *
     * @param context
     * @return
     */

    public static String getClientType(Context context) {
        if (isTablet(context)) {
            return "PAD";
        } else {
            return "Phone";
        }
    }

    private static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;

    }

    /**
     * 获取应用程序名称
     */
    public static String getAppName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return context.getResources().getString(labelRes);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * [获取应用程序版本名称信息]
     *
     * @param context
     * @return 当前应用的版本名称
     */
    public static String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), 0);
            return packageInfo.versionName;

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前进程的名字
     *
     * @param context
     * @return
     */
    public static String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

    /**
     * 获取无线mac地址 不连wifi无法获取
     *
     * @return
     */
    public static String getMac() {
        String macSerial = null;
        String str = "";

        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// 去空格
                    break;
                }
            }
        } catch (IOException ex) {
            // 赋予默认值
            ex.printStackTrace();
        }
        return macSerial;
    }

    /**
     * g获取无线mac地址 不连wifi也可获取
     *
     * @param context
     * @return
     */
    public static String getWIFIMac(Context context) {
        String macAddress = null;
        WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
        if (null != info) {
            macAddress = info.getMacAddress();
        }
        String uuid = (String) SPUtils.get(context, "uuid", UUID.randomUUID().toString());
        SPUtils.put(context, "uuid", uuid);
        return macAddress + "_" + getIMEI(context) + "_" + uuid;
    }

    /**
     * 获取声音的最大值  6.0之后需要适配权限
     *
     * @param context
     * @return
     */

    public static int getMaxVolume(Context context) {

        return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamMaxVolume(AudioManager.STREAM_MUSIC);

    }

    /**
     * 获取当前声音的值  6.0之后需要适配权限
     *
     * @param context
     * @return
     */
    public static int getCurVolume(Context context) {
        return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    /**
     * 设置当前声音的值  6.0之后需要适配权限
     *
     * @param volume
     */
    public static void setVolume(Context context, int volume) {
        ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
    }
}
